#!/bin/bash

PROJECT_NEED_INIT_FLAG=FALSE

PROJECT_TOOL_RUN_ACCESS=TRUE

COMPILE_ROOT_PATH=$(pwd)

function primary_init()
{
	echo "do primary_env_init"
	data_file_temp=`sed -n '/PROJECT_INIT_FILE=/'p data | sed 's/PROJECT_INIT_FILE=//'`
	
	data_path_temp=`sed -n '/PROJECT_INIT_PATH=/'p data | sed 's/PROJECT_INIT_PATH=//'`
	
	cd $COMPILE_ROOT_PATH/$data_path_temp/

	./$data_file_temp INIT
	
	cd $COMPILE_ROOT_PATH
}

function project_init()
{

    echo  *************************need init project********************************
    #从env.dat中找到支持的所有工程
    project_support_temp=`sed -n '/PROJECT_SUPPORT=/'p env.dat | sed 's/PROJECT_SUPPORT=//'`
   
    echo you can choose project is: $project_support_temp
    #输入工程名
    read -p "input project: " input_project_temp
	input_project_temp=`echo $input_project_temp | tr 'a-z' 'A-Z'`
	
	#判断输入的工程名是否合法
	result=$(echo $project_support_temp | grep "${input_project_temp}")
	if [[ $result == "" ]]
	then
	  echo init error,must input valid project name,and exit compile
	  exit 0
	fi
	
	#拷贝shell路径
	env_init_temp=`sed -n '/'"$input_project_temp"_INIT_PATH'=/'p env.dat | sed 's/'"$input_project_temp"_INIT_PATH='//'`

	data_init_temp_new="PROJECT_INIT_PATH=$env_init_temp"


	data_init_temp_old=`sed -n '/PROJECT_INIT_PATH=/'p data`
	
	sed -i "s/$data_init_temp_old/$data_init_temp_new/g" data
	#拷贝相关
	
	
	#拷贝shell文件名
	env_init_temp=`sed -n '/'"$input_project_temp"_INIT_FILE'=/'p env.dat | sed 's/'"$input_project_temp"_INIT_FILE'=//'`

	data_init_temp_new="PROJECT_INIT_FILE=$env_init_temp"

	data_init_temp_old=`sed -n '/PROJECT_INIT_FILE=/'p data`

	sed -i "s/$data_init_temp_old/$data_init_temp_new/g" data
	#拷贝相关
	
	#是否对子环境需要进行初始化
	env_init_temp=`sed -n '/'"$input_project_temp"_INIT_NEED_SHELL'=/'p env.dat | sed 's/'"$input_project_temp"_INIT_NEED_SHELL'=//'`
	
	if [ "$env_init_temp" = "TRUE" ]
	then
		primary_init
	fi
	#替换初始化标记
	project_init_flag_old_temp=`sed -n '/PROJECT_INIT_FLAG=/'p data`
	project_init_flag_new_temp="PROJECT_INIT_FLAG=TRUE"
	
	sed -i "s/$project_init_flag_old_temp/$project_init_flag_new_temp/g" data
	
	echo **************init project:$input_project_temp succeed*********************

}

function input_check()
{
	#检查是否是重新初始化的命令
	input_temp=`echo $1 | tr 'a-z' 'A-Z'`
	if [ "$input_temp" = "INIT" ]
	then
		PROJECT_NEED_INIT_FLAG=TRUE
		return
	fi
	#检查工程是否从未进行初始化
	data_init_flag=`sed -n '/PROJECT_INIT_FLAG=/'p data | sed 's/PROJECT_INIT_FLAG=//'`
	if [ "$data_init_flag" != "TRUE" ]
	then
		PROJECT_NEED_INIT_FLAG=TRUE
		return
	fi
	#检查工程shell文件名称是否进行了初始化
	data_init_check_temp=`sed -n '/PROJECT_INIT_FILE=/'p data | sed 's/PROJECT_INIT_FILE=//'`
	if [ "$data_init_check_temp" = "NULL" ]
	then
		PROJECT_NEED_INIT_FLAG=TRUE
		return
	fi
	#检查工程shell路径名称是否进行了初始化
	data_init_check_temp=`sed -n '/PROJECT_INIT_PATH=/'p data | sed 's/PROJECT_INIT_PATH=//'`
	if [ "$data_init_check_temp" = "NULL" ]
	then
		PROJECT_NEED_INIT_FLAG=TRUE
		return
	fi
}



function project_run()
{
	echo 'do project_run'
	data_file_temp=`sed -n '/PROJECT_INIT_FILE=/'p data | sed 's/PROJECT_INIT_FILE=//'`
	
	data_path_temp=`sed -n '/PROJECT_INIT_PATH=/'p data | sed 's/PROJECT_INIT_PATH=//'`

	cd $COMPILE_ROOT_PATH/$data_path_temp/

	./$data_file_temp
	
	cd $COMPILE_ROOT_PATH
}



function main()
{
  input_check $1
  if [ $PROJECT_NEED_INIT_FLAG = "TRUE" ]
  then
	PROJECT_TOOL_RUN_ACCESS=FALSE
	project_init
  fi
  if [ $PROJECT_TOOL_RUN_ACCESS = "TRUE" ]
  then
	project_run
  fi
}

main $1

exit 0
